//
//  NetWorkView.swift
//  Edaixi-iOS
//
//  Created by 刘树华 on 2017/12/1.
//  Copyright © 2017年 刘树华. All rights reserved.
//

import Foundation
import UIKit
protocol NewWorkEnable {
    
    var startNet : Bool {set get}
    var startNetWithActivity : Bool {set get}
    
}

extension UIButton : NewWorkEnable{
    
    var startNet: Bool {
        set {
            isEnabled = !newValue
        }
        get {
            return !isEnabled
        }
    }
    var startNetWithActivity: Bool {
        set {
            if newValue {
                let activity = UIActivityIndicatorView(activityIndicatorStyle: .white)
                activity.center = CGPoint(x: self.width/2, y: self.height/2)
                self.addSubview(activity)
                activity.startAnimating()
                isEnabled = false
            }else{
               let acArr =  self.subviews.filter{$0 is UIActivityIndicatorView}
                if acArr.count > 0 {
                    acArr[0].removeFromSuperview()
                }
                isEnabled = true
            }
           
            
            
        }
        get {
             let acArr =  self.subviews.filter{$0 is UIActivityIndicatorView}
            if acArr.count > 0  {
                return true
            }else{
                return false
            }
        }
    }
    
}
